import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'


import axios from 'axios'
//配置axios的全局基本路径
/*axios.defaults.baseURL='/api';*/
axios.defaults.baseURL='http://localhost:8080';/*http://172.16.80.248:8080*/
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios;
import routes from './routes'
/*import Mock from './mock'
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'
/**
 * 全局导入百度地图组件
 */
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
    ak: 'UhnSk87Ujbf6GvA8nRekGbxygamwpNdk'//百度地图开发者平台申请的密钥
});

axios.interceptors.request.use(config=>{
    //携带token
    let userToken =  localStorage.getItem("userToken");
    if(userToken){
        config.headers['userToken'] = userToken;
    }
    return config;
},error => {
    Promise.reject(error);
})


/**
 * axios的响应拦截器：
 *  每次发送完请求后，当响应消息传递回来的时候，先判断一下消息是不是noUser,
 *  如果是则直接跳转到登录页面，提示一下“请先登录！”
 */
axios.interceptors.response.use(result=>{
    let data = result.data;
    if(!data.success && data.message==="noUser"){
        alert("请先登录！");
        location.href = "http://localhost:8081/#/login";
    }
    return result;
},error => {
    Promise.reject(error);
})

Vue.use(ElementUI);
Vue.use(VueRouter);
Vue.use(Vuex);

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
});

/*
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login') {
    sessionStorage.removeItem('user');
  }
  let user = JSON.parse(sessionStorage.getItem('user'));
  if (!user && to.path != '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})
*/

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app');

